﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ModelSettings = PasswordProvider.ObjectModel.Properties.Settings;
using PasswordProvider.ViewModel.Properties;
using PasswordProvider.ViewModel.Framework;
using System.Globalization;

namespace PasswordProvider.ViewModel
{
    public class SecurityOptionsViewModel : VisualViewModel, ICommitChanges
    {
        internal SecurityOptionsViewModel()
            : base(Strings.SecurityOptions_DisplayName, Strings.SecurityOptions_ToolTip)
        { }

        #region ICommitChanges

        /// <summary>
        /// Write the settings values to persistant storage.
        /// </summary>
        public void CommitChanges()
        {
            if (this.AuthenticationInvalidationMethod.IsChanged)
                ModelSettings.Default.AuthenticationInvalidationMethod =
                    (ObjectModel.AuthenticationInvalidationMethods)AuthenticationInvalidationMethod.EnumValue;

            if (this.ValidationTimeoutMinutes.IsChanged)
                ModelSettings.Default.ValidationTimeoutMinutes = Double.Parse(ValidationTimeoutMinutes.Text, CultureInfo.InvariantCulture);

            if (this.EncryptDataFile.IsChanged)
                ModelSettings.Default.EncryptDataFile = EncryptDataFile.IsChecked;

            ModelSettings.Default.Save();
        }
        #endregion

        #region Public

        /// <summary>
        /// List view sorting method option
        /// </summary>
        public EnumSelectorViewModel AuthenticationInvalidationMethod
        {
            get
            {
                if (_authenticationInvalidationMethod == null)
                {
                    _authenticationInvalidationMethod = new EnumSelectorViewModel(
                        Strings.SecurityOptions_InvalidationMethod_DisplayName,
                        Strings.SecurityOptions_InvalidationMethod_ToolTip,
                        ModelSettings.Default.AuthenticationInvalidationMethod);
                    _authenticationInvalidationMethod.DisplayItems.Add(
                        new EnumSelectorDisplayItemViewModel(
                            Strings.SecurityOptions_InvalidationMethod_InvalidateNever,
                            Strings.SecurityOptions_InvalidationMethod_InvalidateNever_ToolTip,
                            ObjectModel.AuthenticationInvalidationMethods.None));
                    _authenticationInvalidationMethod.DisplayItems.Add(
                        new EnumSelectorDisplayItemViewModel(
                            Strings.SecurityOptions_InvalidationMethod_InvalidateAfterAccess,
                            Strings.SecurityOptions_InvalidationMethod_InvalidateAfterAccess_ToolTip,
                            ObjectModel.AuthenticationInvalidationMethods.InvalidateAfterAccess));
                    _authenticationInvalidationMethod.DisplayItems.Add(
                        new EnumSelectorDisplayItemViewModel(
                            Strings.SecurityOptions_InvalidationMethod_InvalidateByTimeout,
                            Strings.SecurityOptions_InvalidationMethod_InvalidateByTimeout_ToolTip,
                            ObjectModel.AuthenticationInvalidationMethods.InvalidateAfterTimeout));
                    _authenticationInvalidationMethod.EnumValueChanged += new ValueChangedEventHandler<Enum>(AuthenticationInvalidationMethod_ValueChanged);
                }
                return _authenticationInvalidationMethod;
            }
        } EnumSelectorViewModel _authenticationInvalidationMethod;

        /// <summary>
        /// ValidationTimeoutMinutes option
        /// </summary>
        public TextEditViewModel ValidationTimeoutMinutes
        {
            get
            {
                if (_validationTimeOut == null)
                {
                    _validationTimeOut = new TextEditViewModel(
                        Strings.SecurityOptions_ValidationTimeoutMinutes_DisplayName,
                        Strings.SecurityOptions_ValidationTimeoutMinutes_ToolTip,
                        Convert.ToString(ModelSettings.Default.ValidationTimeoutMinutes, CultureInfo.CurrentCulture));                    
                }
                return _validationTimeOut;
            }
        } private TextEditViewModel _validationTimeOut;

        public ToggleViewModel EncryptDataFile
        {
            get
            {
                if (_encryptDataFile == null)
                {
                    _encryptDataFile = new ToggleViewModel(
                        Strings.SecurityOptions_EncryptDataFile_DisplayName,
                        Strings.SecurityOptions_EncryptDataFile_ToolTip,
                        ModelSettings.Default.EncryptDataFile);
                }
                return _encryptDataFile; 
            }
            
        } private ToggleViewModel _encryptDataFile;

        #endregion // Public Interface

        #region Private Methods

        void AuthenticationInvalidationMethod_ValueChanged(Enum newValue)
        {
            SetTimeOutEnabledStatus((ObjectModel.AuthenticationInvalidationMethods)newValue);
        }
        
        private void SetTimeOutEnabledStatus(ObjectModel.AuthenticationInvalidationMethods method)
        {
            ValidationTimeoutMinutes.IsEnabled =
                (method & ObjectModel.AuthenticationInvalidationMethods.InvalidateAfterTimeout) ==
                    ObjectModel.AuthenticationInvalidationMethods.InvalidateAfterTimeout;
        }


        #endregion // Private members
    }
}
